#!/bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2003, 2005
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#
#
#  FILE   : ftp
#
#  PURPOSE: Ftp into a remote hosts successfully as a vaild user (other than root)
#
#  HISTORY:
#     03/03  Jerone Young (jeroney@us.ibm.com)
#     04/03  Dustin Kirkland (k1rkland@us.ibm.com)
#     09/05  Kris Wilson (krisw@us.ibm.com) Check if vsftpd.conf was found.
#
#  NOTE:
#	This version is intended for EAL certification, it will need modification
#	to conform with LTP standards in the offical LTP tree.

#-----------------------------------------------------------------------
# FUNCTION:  do_setup
#-----------------------------------------------------------------------

do_setup()
{

    RHOST=${RHOST}
    TEST_USER="ftpuser1"
    TCtmp=/home/$TEST_USER
    TEST_USER_PASSWD="eal"
    TEST_USER_ENCRYPTED_PASSWD="42VmxaOByKwlA"
    TEST_USER_HOMEDIR="/home/$TEST_USER"
    VSFTP_CONF="/etc/vsftpd/vsftpd.conf"

    tst_setup

    exists ftp useradd userdel vsftpd

    LOCAL_ENABLE=$(/usr/bin/rsh -l root -n $RHOST "awk '/^local_enable=/' $VSFTP_CONF")
    LOCAL_ENABLE_VALUE=$(echo $LOCAL_ENABLE | awk -F= '{print $2}')

    [ "$LOCAL_ENABLE_VALUE" != "YES" ] && LOCAL_ENABLE_VALUE="NO"

    /usr/bin/rsh -l root -n $RHOST "/usr/sbin/userdel -rf $TEST_USER"
    sleep 1

    if ! /usr/bin/rsh -l root -n $RHOST "/usr/sbin/useradd -m -p $TEST_USER_ENCRYPTED_PASSWD $TEST_USER"; then
        end_testcase "Could not add test user $TEST_USER on system $RHOST."
    fi

    if ! /usr/bin/rsh -l root -n $RHOST "/etc/init.d/vsftpd status | grep pid || /etc/init.d/vsftpd start"; then
        end_testcase "Could not start vsftpd service on $RHOST."
    fi


    trap do_cleanup INT EXIT

}

#-----------------------------------------------------------------------
# FUNCTION:  do_cleanup
#-----------------------------------------------------------------------

do_cleanup()
{
    /usr/bin/rsh -l root -n $RHOST "/usr/sbin/userdel -rf $TEST_USER"
    tst_cleanup
}

#-----------------------------------------------------------------------
# FUNCTION:  do_test
#
# DESCRIPTION: The test user will ftp in and create a directory in his home directory on the remote host.
#              The directory is then checked on the remote hosts to see if it is owned
#	       by the test user.
#-----------------------------------------------------------------------

do_test(){
    echo "TINFO | TEST: Ftp into a remote host as a local user (other than root), LOCAL_ENABLE=$LOCAL_ENABLE_VALUE"
    TEST="ftp03"

    if [ "$LOCAL_ENABLE_VALUE" = "YES" ]; then
	expect -c "
            spawn ftp $RHOST
            sleep 1
            expect -re \": \"
            send \"$TEST_USER\r\"
            expect -re \"Password:\"
            send \"$TEST_USER_PASSWD\r\"
            expect {
                # 530 - Login failed
                \"530\" {send_user \"==> TEST \#$TEST : FAIL (ftp rejected login attempt)\n\";exit 1}
                # 230 - Login successful
                \"230\" {send_user \"==> TEST \#$TEST : PASS (ftp allowed login attempt)\n\";exit 0}
            }
            expect \"ftp> \"
            send \"quit\r\"
	"
    else
        expect -c "
            spawn ftp $RHOST
            sleep 1
            expect -re \": \"
            send \"$TEST_USER\r\"
            expect -re \"Password:\"
            send \"$TEST_USER_PASSWD\r\"
            expect {
                # 230 - Login successful
                \"230\" {send_user \"==> TEST \#$TEST : FAIL (ftp allowed login attempt)\n\";exit 1}
                # 500 - Login failed
                \"500\" {send_user \"==> TEST \#$TEST : PASS (ftp rejected login attempt)\n\";exit 0}
                # 530 - Login failed
                \"530\" {send_user \"==> TEST \#$TEST : PASS (ftp rejected login attempt)\n\";exit 0}
            }
            expect \"ftp> \"
            send \"quit\r\"
        "
    fi

    if [ $? != 0 ]; then
        end_testcase "Testcase failed"
    fi
}

#----------------------------------------------------------------------
# FUNCTION: MAIN
# PURPOSE:  To invoke the functions to perform the tasks described in
#           the prologue.
#----------------------------------------------------------------------
# include lib files
if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/lib/net_cmdlib.sh
. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}


read_opts $*
setup_env || { echo "setup env failed, test end."; exit 1; }


do_setup
do_test
end_testcase
EchoResult "ftp03 testcase."
